package org.example.innovate.tool;

import org.w3c.dom.stylesheets.LinkStyle;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Random;

public class addData {
    public static void main(String[] args) throws IOException {
        String folderPath = "D:\\python\\project\\food_test_2\\train\\data"; // 文件夹路径
        File folder = new File(folderPath);
        ArrayList<String> linesToAdd = new ArrayList<>();
        traverseFiles(folder,linesToAdd,"",0);
        String filePath = "src/main/resources/static/sql/insert_dish_recipe.sql"; // 文件路径


        try (FileWriter fw = new FileWriter(filePath, true); // 使用true作为第二个参数以追加模式打开文件
             BufferedWriter bw = new BufferedWriter(fw)) {
            for (String line : linesToAdd) {
                bw.write(line);
                bw.newLine(); // 写入一个新行
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void traverseFiles(File folder,ArrayList<String> list,String name,int num) throws IOException {
        StringBuilder base_dish= new StringBuilder("insert into dish (user_id, name, category, description_url, image_url, likes, favorites, views) values (100000,");
        StringBuilder base_recipe= new StringBuilder("insert into recipe(dish_id,image_url) VALUES (");
        String base_url= "";
        File[] files = folder.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    num++;
                    name = file.getName();
                    String des_url="https://xiaoxiaobaiyang.oss-cn-shenzhen.aliyuncs.com/food_moment_discover/train/data/"+name+"/menu.txt";
                    URL url = new URL(des_url);
                    BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
                    String dish_name="";
                    ArrayList<String> dish_cate=new ArrayList<>();
                    boolean flag=true;
                    String line;
                    while ((line = reader.readLine()) != null) {
                        if(line.equals("*****"))
                        {
                            if (!flag)
                            {
                                break;
                            }
                            flag=false;
                        }
                        if(flag)
                        {
                            dish_name=line;
                        }
                        else if(!line.equals("*****")){
                            dish_cate.add(line);
                        }
                    }
                    reader.close();
                    String dish_category="";
                    for (String category : dish_cate) {
                        dish_category += category;
                        dish_category += ",";
                    }
                    String image_url=base_url+name+"/main.jpg";
                    Random random = new Random();
                    // 生成一个随机整数
                    int likes = random.nextInt(500);
                    int fav=random.nextInt(500);
                    int view=random.nextInt(500);
                    StringBuilder dish=new StringBuilder();
                    dish=base_dish;
                    dish.append("'").append(dish_name).append("',");
                    dish.append("'").append(dish_category).append("',");
                    dish.append("'").append(des_url).append("',");
                    dish.append("'").append(image_url).append("',");
                    dish.append(likes).append(",");
                    dish.append(fav).append(",");
                    dish.append(view).append(");");
                    list.add(dish.toString());
                    base_dish= new StringBuilder("insert into dish (user_id, name, category, description_url, image_url, likes, favorites, views) values (100000,");
                    traverseFiles(file,list,name,num); // 递归调用，遍历子文件夹中的文件
                } else {
                    if(!file.getName().equals("main.jpg")&&!file.getName().equals("menu.txt")){
                        String cai_url=base_url+name+"/"+file.getName();
                        StringBuilder recipe=new StringBuilder();
                        recipe=base_recipe;
                        recipe.append(num).append(",");
                        recipe.append("'").append(cai_url).append("');");
                        list.add(recipe.toString());
                        base_recipe= new StringBuilder("insert into recipe(dish_id,image_url) VALUES (");
                    }
                }
            }
        }
    }
}
